// 引入 request 方法，假设 request 方法已经定义在某个工具文件中
import request from '/@/utils/request'; 
// 定义 API 路径枚举
enum Api {
    SysCacheBeginCacheLock = '/sysCache/beginCacheLock/{key}/{msTimeout}/{msExpire}/{throwOnFailure}',
    SysCacheClear = '/sysCache/clear',
    SysCacheDeleteByPreKey = '/sysCache/deleteByPreKey/{prefixKey}',
    SysCacheDeleteKey = '/sysCache/delete/{key}',
    SysCacheKeyList = '/sysCache/keyList',
    SysCacheKeysByPrefixKey = '/sysCache/keysByPrefixKey/{prefixKey}',
    SysCacheValueKey = '/sysCache/value/{key}',
}

// 定义 API 方法
/**
 * 申请分布式锁
 * @param key 要锁定的key
 * @param msTimeout 申请锁等待的时间，单位毫秒
 * @param msExpire 锁过期时间，超过该时间没有主动释放则自动释放，必须整数秒，单位毫秒
 * @param throwOnFailure 失败时是否抛出异常，如不抛出异常，可通过判断返回null得知申请锁失败
 */
export const apiSysCacheBeginCacheLockKeyMsTimeoutMsExpireThrowOnFailurePost = (
    key: string,
    msTimeout: number,
    msExpire: number,
    throwOnFailure: boolean
) =>
    request({
        url: Api.SysCacheBeginCacheLock
            .replace('{key}', encodeURIComponent(key))
            .replace('{msTimeout}', encodeURIComponent(String(msTimeout)))
            .replace('{msExpire}', encodeURIComponent(String(msExpire)))
            .replace('{throwOnFailure}', encodeURIComponent(String(throwOnFailure))),
        method: 'post',
    });

/**
 * 清空所有缓存 🔖
 */
export const apiSysCacheClearPost = () =>
    request({
        url: Api.SysCacheClear,
        method: 'post',
    });

/**
 * 根据键名前缀删除缓存 🔖
 * @param prefixKey 键名前缀
 */
export const apiSysCacheDeleteByPreKeyPrefixKeyPost = (prefixKey: string) =>
    request({
        url: Api.SysCacheDeleteByPreKey.replace('{prefixKey}', encodeURIComponent(prefixKey)),
        method: 'post',
    });

/**
 * 删除缓存 🔖
 * @param key 缓存键
 */
export const apiSysCacheDeleteKeyPost = (key: string) =>
    request({
        url: Api.SysCacheDeleteKey.replace('{key}', encodeURIComponent(key)),
        method: 'post',
    });

/**
 * 获取缓存键名集合 🔖
 */
export const apiSysCacheKeyListGet = () =>
    request({
        url: Api.SysCacheKeyList,
        method: 'get',
    });

/**
 * 根据键名前缀获取键名集合 🔖
 * @param prefixKey 键名前缀
 */
export const apiSysCacheKeysByPrefixKeyPrefixKeyGet = (prefixKey: string) =>
    request({
        url: Api.SysCacheKeysByPrefixKey.replace('{prefixKey}', encodeURIComponent(prefixKey)),
        method: 'get',
    });

/**
 * 获取缓存值 🔖
 * @param key 缓存键
 */
export const apiSysCacheValueKeyGet = (key: string) =>
    request({
        url: Api.SysCacheValueKey.replace('{key}', encodeURIComponent(key)),
        method: 'get',
    });